<template>
  <div class="malfunction">
    <a-row :gutter="[16, 16]">
      <a-col>
        <div class="starLinkMap">
          <h3 class="item-title">星链图</h3>
          <div class="star-map-container">
            <starMap></starMap>
          </div>
        </div>
      </a-col>
    </a-row>
    <a-row :gutter="[16, 16]">
      <a-col :span="6">
        <div class="chart-item">
          <h3 class="item-title">设备告警信息</h3>
          <div class="item-container">
            <a-table
              :columns="columns"
              :rowKey="
                (record, index) => {
                  return index;
                }
              "
              :data-source="device_error"
              :pagination="false"
            >
            </a-table>
          </div>
        </div>
      </a-col>
      <a-col :span="12">
        <div class="chart-item">
          <h3 class="item-title">数据分析</h3>
          <div class="item-container error-container">
            <div class="chart-left">
              <h3>设备状态</h3>
              <a-progress
                type="circle"
                :width="150"
                status="active"
                :percent="calcDeviceError"
              >
                <template #format="percent">
                  <span>{{ percent }}%</span>
                </template></a-progress
              >
              <div class="text-item">
                <div class="text-item-1">
                  <p>正常设备</p>
                  <p>{{ star_error.normal }}</p>
                </div>
                <div class="text-item-1">
                  <p>异常设备</p>
                  <p>{{ star_error.abnormal }}</p>
                </div>
              </div>
            </div>
            <div class="chart-right">
              <h3>故障数量统计</h3>
              <div id="main" style="width:100%;height:200px;"></div>
            </div>
          </div>
        </div>
      </a-col>
      <a-col :span="6">
        <div class="chart-item">
          <h3 class="item-title">线路告警信息</h3>
          <ul class="item-ul" v-if="line_error_visible">
            <li
              class="item-box"
              v-for="(item, index) in line_error"
              :key="index"
            >
              <div class="left-item">
                <img :src="item.url" alt="" />
              </div>
              <div class="right-item">
                <p>星链ID：{{ item.starlinkId }}</p>
                <p>内容详情：{{ item.alarm_msg }}</p>
                <p>告警等级：{{ item.level }}</p>
                <p>告警时间：{{ item.time }}</p>
              </div>
            </li>
          </ul>
          <a-empty style="padding-top:50px;" v-if="!line_error_visible" />
        </div>
      </a-col>
    </a-row>
  </div>
</template>

<script>
import { mapState } from 'vuex';
import * as echarts from 'echarts';
import { getStatLinkErrorNum } from '@/services/user';
import starMap from '../../components/starMap/starMap.vue';
import {
  getStatLinkError,
  getLineAlarm,
  getDeviceAlarm,
} from '@/services/user';
export default {
  name: 'Demo',
  i18n: require('./i18n'),
  components: {
    starMap,
  },
  data() {
    return {
      line_error_visible: false,
      pagination: {
        total: 40,
      },
      columns: [
        {
          title: '设备ID',
          dataIndex: 'starlinkId',
          key: 'starlinkId',
        },
        {
          title: '告警类型',
          dataIndex: 'type',
          key: 'type',
        },
        {
          title: '告警时间',
          dataIndex: 'time',
          key: 'time',
        },
      ],
      star_error: {
        normal: 0,
        abnormal: 0,
      },
      line_error: [{}, {}],
      device_error: [],
      // 车站位置
      // 上行星链设备位置
      // 下行星链设备位置
    };
  },
  computed: {
    ...mapState('setting', ['pageMinHeight']),
    desc() {
      return this.$t('description');
    },
    calcDeviceError() {
      let num =
        this.star_error.abnormal /
        (this.star_error.normal + this.star_error.abnormal);
      num = parseInt(num * 100);
      return num;
    },
  },
  mounted() {
    getStatLinkError().then((res) => {
      this.star_error = res.data.result[0];
    });
    getLineAlarm({
      starlinkId: 11777,
      start: '2020-01-01 00:00:00',
      end: '2021-07-13 10:19:04',
      page: 1,
      pagesize: 3,
    }).then((res) => {
      if (res.data.code === 200 && res.data.total_num !== 0) {
        this.line_error_visible = true;
        this.line_error = res.data.result;
      } else {
        this.line_error_visible = false;
        this.line_error = [];
      }
    });
    getDeviceAlarm({
      number: 3,
    }).then((res) => {
      if (res.data.total_num !== 0 && res.data.code === 200) {
        this.device_error = res.data.result;
      } else {
        this.device_error = [];
        // 告警信息如果为空 则显示 空信息站位
      }
    });
    getStatLinkErrorNum({
      start: '2020-05-01 00:00:00',
      end: '2021-05-12 00:00:00',
    }).then(this.afterStatLinkErrorNum);
  },
  methods: {
    afterStatLinkErrorNum(res) {
      //  重新渲染图表
      var myChart = echarts.init(document.getElementById('main'));
      // 绘制图表
      myChart.setOption({
        tooltip: {},
        xAxis: {
          type: 'category',
          name: '日期',
          data: this.transitionArr(res.data.result, 'xAxis'),
        },
        yAxis: {
          name: '数量',
        },
        series: [
          {
            name: '数量',
            type: 'bar',
            barWidth: 10,
            data: this.transitionArr(res.data.result, 'error_count'),
          },
        ],
      });
    },
    transitionArr(arr, type) {
      if (arr.length === 0) {
        return [];
      }
      let returnArr = [];
      switch (type) {
        case 'type_count':
          arr.forEach((item) => {
            returnArr.push(item.type_count);
          });
          return returnArr;
        case 'error_count':
          arr.forEach((item) => {
            returnArr.push(item.error_count);
          });
          return returnArr;
        case 'xAxis':
          arr.forEach((item) => {
            returnArr.push(item.day_time);
          });
          return returnArr;
      }
    },
    // 获取设备告警信息
    // 获取线路告警信息
  },
};
</script>

<style scoped lang="less">
.starLinkMap {
  height: calc(100vh - 300px - 40px - 65px); // 星链图的高度
  background: #fff;
}
.demo-img {
  display: block;
  width: 100%;
  height: 400px;
}
.malfunction {
  .chart-item {
    background: #fff;
    height: 300px; // 底部模块高度
    overflow: hidden;
  }
  .item-container {
    padding: 0 16px 16px;
  }
  .error-container {
    width: 100%;
    padding: 20px 20px 0 20px;
    color: #535875;
    h3 {
      color: #535875;
    }
    .chart-left {
      height: 200px;
      width: 280px;
      display: inline-block;
      vertical-align: top;
    }
    .chart-right {
      height: 200px;
      width: calc(100% - 280px);
      display: inline-block;
      vertical-align: top;
    }
    .text-item {
      width: 100px;
      display: inline-block;
      vertical-align: top;
      text-align: center;

      padding-top: 20px;
    }
  }
  .card-box {
    height: 300px;
    background: #f6f6f6;
    box-shadow: 2px 2px 6px #e5e5e5;
    border-radius: 10px;
  }
  .item-ul {
    background: #fff;
    margin: 0;
    padding: 0 0 10px;
  }
  .item-box {
    display: flex;
    margin: 10px 10px 0;
    padding: 10px;
    background: #f6f6f6;
    p {
      margin: 0;
      // line-height: 22px;
    }
    .left-item {
      width: 140px;
      img {
        width: 140px;
        height: 86px;
      }
    }
    .right-item {
      width: calc(100% -140px);
      box-sizing: border-box;
      padding-left: 10px;
    }
  }
}
</style>
